﻿using System;
using System.Drawing;
using System.Windows.Forms;
using XCI.Sys.Helper;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;
using XCI.Windows.Extensions;
using XCI.Windows.Helper;

namespace XCI.Sys.Forms
{
    public partial class FrmUserDetails : FrmSysBase
    {
        private readonly GridHelper<SysUser> helper;
        private GridHelper<SysRole> roleHelper;
        private TreeHelper<SysModule> moduleTreeHelper;
        private ModuleHelper moduleHelper;
        private TabDelayLoad tabDelayLoad;
        private SysUser model;
        private string id;

        public FrmUserDetails(string p_id)
        {
            InitializeComponent();
            id = p_id;
        }

        public FrmUserDetails(GridHelper<SysUser> p_helper)
        {
            InitializeComponent();
            helper = p_helper;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keys)
        {
            return OnEsc(msg, keys, DetailsFormEscResultCancel);
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        protected void InitForm()
        {
            roleHelper = new GridHelper<SysRole>(roleGridView);
            moduleHelper = new ModuleHelper(treeListx1);
            moduleTreeHelper = new TreeHelper<SysModule>(treeListx1);
            editDept.Properties.Appearance.Font = new Font(Font.Name, Font.Size, FontStyle.Underline);

            BindModel();
        }

        /// <summary>
        /// 绑定数据到界面
        /// </summary>
        private void BindModel()
        {
            if (helper != null)
            {
                id = helper.SelectedId;
                btnPrev.Enabled = !helper.IsFirstFocus;
                btnNext.Enabled = !helper.IsLastFocus;
            }
            model = UserService.Instance.SelectById(id);
            tabDelayLoad = new TabDelayLoad(tabPane1, TabLoad);
        }

        /// <summary>
        /// 选项卡加载处理函数
        /// </summary>
        /// <param name="name">选项卡名称</param>
        protected void TabLoad(string name)
        {
            switch (name)
            {
                case nameof(tabBasic):
                    //查询基本信息
                    BindDetailsPanel(model, layoutPanel);
                    BindLastOperateUser(model?.Id, editOperateUserName, editOperateDateTime);
                    break;
                case nameof(tabUserMapRole):
                    //查询当前用户拥有的角色成员
                    if (model != null)
                    {
                        roleHelper.RunCallback(e =>
                        {
                            e.Result = RoleService.Instance.SelectListByUserId(model.Id);
                        })
                        .RunAsync();
                    }
                    else
                    {
                        roleHelper.SetDataSource(null);
                    }
                    break;
                case nameof(tabUserPermission):
                    //查询当前用户拥有的权限模块
                    if (model != null)
                    {
                        moduleTreeHelper.RunCallback(e =>
                        {
                            e.Result = ModuleService.Instance.SelectListByUserId(model.Id);
                        })
                        .CompletCallback(e =>
                        {
                            moduleTreeHelper.SetDataSource(e.Result);
                            moduleHelper.InitModuleImageList(treeImageList, false);
                            treeListx1.ExpandAll();
                        })
                        .RunAsync();
                    }
                    else
                    {
                        moduleTreeHelper.SetDataSource(null);
                    }
                    break;
                case nameof(tabHistory):
                    //历史记录
                    recordHistoryGridUserControl1.PrimaryKey = model?.Id;
                    break;
            }
        }

        private void FrmUserDetails_Load(object sender, EventArgs e)
        {
            InitForm();
        }

        private void TreeListx1_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e)
        {
            e.NodeImageIndex = moduleHelper.GetModuleImageIndex(e.Node);
        }

        private void BtnPrev_Click(object sender, EventArgs e)
        {
            helper.View.MovePrev();
            BindModel();
        }

        private void BtnNext_Click(object sender, EventArgs e)
        {
            helper.View.MoveNext();
            BindModel();
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            ResultCancel();
        }

        private void EditDept_DoubleClick(object sender, EventArgs e)
        {
            new FrmDeptDetails(model.DeptId).ShowDialogOk();
        }

        private void RoleGridView_DoubleClick(object sender, EventArgs e)
        {
            helper.DblClickRow(e, () =>
            {
                new FrmRoleDetails(roleHelper.SelectedId).ShowDialogOk();
            });
        }
    }
}
